package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign.question;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.util.YxyResUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin
public class HomeworkGetBookcontentKnowController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyResUtil yxyResUtil;

    @ApiOperation(value = "查询章节的知识点和试题的交集知识点")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workassign/get/bcknowqueskno", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws JsonParseException, JsonMappingException, IOException {
        RespModel respModels = new RespModel();
        // 找到这个目录下的所有子目录
        List<String> bookcontentList = yxyResUtil.findTocByTocParent(reqModel.bookcontent);
        if (bookcontentList.size() > 0) {
            List<YxyResBookContentsKnoledges> bookContentsKnoledges = yxyResBookContentsKnoledgesRepository
                    .findByBookContentIdIn(bookcontentList);
            List<Integer> bcKnowList = bookContentsKnoledges.stream().map(x -> x.getKnowledgeId())
                    .collect(Collectors.toList());
            Set<Integer> set = new HashSet<Integer>();
            set.addAll(bcKnowList);
            set.retainAll(reqModel.quesKnow);
            bcKnowList.clear();
            bcKnowList.addAll(set);
            respModels.knowId = bcKnowList;
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String bookcontent;
        public List<Integer> quesKnow;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(bookcontent, null), "章节不能为空");
            Assert.isTrue(quesKnow != null && quesKnow.size() > 0, "试题没有知识点");
        }
    }

    protected static class RespModel {
        public List<Integer> knowId = new ArrayList<Integer>();
    }
}
